package tgc.edu.tms.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import tgc.edu.tms.custom.CommonController;
import tgc.edu.tms.entity.CourseCategory;
import tgc.edu.tms.entity.Schoolyear;
import tgc.edu.tms.web.form.CourseCategoryForm;

@Controller
@RequestMapping(value = "/dictionary/courseCategory")
public class CourseCategoryController extends CommonController<CourseCategory, Integer, CourseCategoryForm>{

	@Override
	public Specification<CourseCategory> buildSpec(CourseCategoryForm form) {
		Specification<CourseCategory> spec=new Specification<CourseCategory>() {
			
			@Override
			public Predicate toPredicate(Root<CourseCategory> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				List<Predicate> rules=new ArrayList<>();
				if (StringUtils.hasText(form.getSearch())) {
					Predicate like = criteriaBuilder.like(root.get("type"), "%" + form.getSearch() + "%");
					rules.add(like);
				}
				return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}

}
